<?php

class Catalogue_Model_Object_Observer_CategoryPriority extends Workset_Model_Observer_Abstract {

	private $_value = null;

	function beforeUnlinkObject(Workset_Model_Subject_Interface $object, $category = null)
	{
		$this->_value = null;

		if ($category instanceof Catalogue_Model_Category) {
			$this->_value = $object->getCategoryPriority($category);
		}
	}

	function afterUnlinkObject(Workset_Model_Subject_Interface $object, $category = null)
	{
		if (null !== $this->_value && $category instanceof Catalogue_Model_Category) {
			$where = array(
				'priority > ?' => $this->_value
			);

			//базовая категория не учавствует в сортировке
			if ($object->hasCategory()) {
				$where['category_id != ?'] = $object->getCategoryId();
			}

			$object->updateCategoryPriority($where, -1);
		}

		$this->_value = null;
	}

}